import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import Enroll from "../views/Enroll.vue";
import Home from "../views/Home.vue";
import MainPage from "../views/MainPage.vue";
import HotTopic from "../views/HotTopic.vue";
import Mymes from "../views/Mymes.vue";
import Mycenter from "../views/Mycenter.vue";
import Create from "../views/CreationCenter.vue";
import Details from "../views/DetailsPage/Details.vue";
import NewsPage from "../views/DetailsPage/NewsPage.vue";
import VideoPage from "../views/DetailsPage/VideoPage.vue";
import ArticlePage from "../views/DetailsPage/ArticlePage.vue";
import Search from "../views/Search.vue";
import Rescource from "../views/Resource.vue";
import Collect from "../views/Collect.vue";
import History from "../views/History.vue";
import UserRoom from "../views/UserRoom.vue";
import Issue from "../views/IssueCenter.vue";

Vue.use(VueRouter);
const routes = [
  { path: "/login", name: "login", component: Login },
  { path: "/enroll", name: "Enroll", component: Enroll },
  { path: "/Search", name: "Search", component: Search },
  { path: "/Rescource", name: "Rescource", component: Rescource },
  {
    path: "/UserRoom",
    name: "UserRoom",
    component: UserRoom,
    children: [{ path: "Issue", name: "Issue", component: Issue }],
  },
  {
    path: "/",
    name: "mainpage",
    component: MainPage,
    children: [
      { path: "/", name: "Home", component: Home },
      { path: "hottopic", name: "hotTopic", component: HotTopic },
      { path: "/Collect", name: "Collect", component: Collect },
      { path: "/History", name: "History", component: History },
    ],
  },
  { path: "/Mymes", name: "Mymes", component: Mymes },
  { path: "/Mycenter", name: "Mycenter", component: Mycenter },
  { path: "/Create", name: "Create", component: Create },
  {
    path: "/Details",
    name: "Details",
    component: Details,
    children: [
      { path: "news", name: "new", component: NewsPage },
      { path: "video", name: "video", component: VideoPage },
      { path: "article", name: "article", component: ArticlePage },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 前置路由守卫（路由导航）
// var routerArr = [
//   "/login",
//   "/enroll",
//   "/Search",
//   "/",
//   "hottopic",
//   "Details",
//   "Rescource",
// ];
// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem("token");
//   routerArr.forEach((e) => {
//     if (to.path !== e) {
//       if (token) {
//         next();
//       } else {
//         alert("请先登录！");
//         setTimeout(() => {
//           next("/login");
//         }, 500);
//       }
//       return;
//     }
//   });
//   next();
// });

// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem("token");
//   if (to.path !== "/login") {
//     if (to.path !== "/enroll") {
//       if (token) {
//         next();
//       } else {
//         alert("请先登录！");
//         setTimeout(() => {
//           next("/login");
//         }, 500);
//       }
//       return;
//     }
//   }
//   next();
// });
router.beforeEach((to, from, next) => {
  // 让页面回到顶部
  document.documentElement.scrollTop = 0;
  // 调用 next()，一定要调用 next 方法，否则钩子就不会被销毁
  next();
});

export default router;
